import 'package:flutter/material.dart';

// void main() {
//   runApp(MaterialApp(
//     title: "TabBarWidget",
//     home: MyApp(),
//   ));
// }

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
  late TabController _tabController;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(vsync: this, length: 4);
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        child: Scaffold(
      // appBar: AppBar(
      //   automaticallyImplyLeading: false,
      //   // title: Text("Tab切换"),
      //   bottom: TabBar(
      //     tabs: <Widget>[
      //       Tab(text: "热门"),
      //       Tab(text: "推荐"),
      //       Tab(text: "关注"),
      //       Tab(text: "收藏"),
      //       Tab(text: "新增"),
      //       Tab(text: "点赞"),
      //     ],
      //     controller: _tabController, // 记得要带上tabController
      //   ),
      // ),
      appBar: PreferredSize(
          child: AppBar(
            toolbarHeight: 0.6,
            elevation: 0,
            backgroundColor: Colors.white,
            automaticallyImplyLeading: false,
            bottom: TabBar(
              indicatorSize: TabBarIndicatorSize.label,
              indicatorWeight: 3.0,
              labelColor: Colors.black,
              indicatorColor: Colors.red,
              tabs: <Widget>[
                Tab(text: "热门"),
                Tab(text: "推荐"),
                Tab(text: "关注"),
                Tab(text: "收藏"),
              ],
              controller: _tabController, // 记得要带上tabController
            ),
          ),
          preferredSize: Size.fromHeight(50.0)),
      body: TabBarView(
        controller: _tabController,
        children: <Widget>[
          Center(child: Text("这是热门的内容")),
          Center(child: Text("这是推荐的内容")),
          Center(child: Text("这是关注的内容")),
          Center(child: Text("这是收藏的内容")),
        ],
      ),
    ));
  }
}
